const koa = require('koa')//引入koa
const mongoose = require('mongoose')//引入mongoDB
const bodyParser = require('koa-bodyparser')//引入接收前端传来数据模块
const passport = require('koa-passport')//生成证书令牌
const cors = require('koa2-cors');
// const CrosPlugin = require("./cros");

//实例化koa
const app = new koa() //实例化koa
app.use(cors());
// CrosPlugin(app)

//引入外接数据库地址
const db = require('./config/keys')//引入数据库秘钥

//使用koa-bodyparser
app.use(bodyParser()) //使用接收前端传来数据模块


//链接数据库
mongoose.connect(db.mongoURL, db.mongoconfig).then(() => {
    console.log('mongodb connect...');
}).catch(err => {
    console.log(err);
})

//回调到config文件中 passport.js
require("./config/passport")(passport)

//签证证书
app.use(passport.initialize()) //初始化签证
app.use(passport.session())//存储签证

// 引入路由
const router = require('./routes/router/index')

//配置路由
app.use(router.routes())

//设置端口号
// const port = process.env.PORT || 5000

//监听端口号
app.listen(db.port, () => {
    console.log(`server started on ${db.port}`);
    console.log(`http://localhost:${db.port}`);
})
